<?php


namespace App\Api\Controllers;


use App\Models\Vote;
use App\Models\VoteLog;
use App\Models\VotePlayer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VoteController extends Controller
{
    public function vote()
    {
        $vote = Vote::getVoting(1);
        if ($vote) {
            return $this->responseData($vote);
        } else {
            $vote = Vote::getVoting(0);
            return $this->responseData($vote);
        }
    }

    //参赛者列表
    public function votePlayer(Request $request)
    {
        if (!$request->vote_id) {
            return $this->responseFailed('vote_id必传');
        }
        $where = [
            ['vote_id', '=', $request->vote_id]
        ];
        if (!empty($request->search)) {
            $search = $request->search;
            if (is_numeric($search)) {
                $where[] = ['no', '=', $search];
            } else {
                $where[] = ['player_name', 'like', '%'.$search.'%'];
            }
        }
        $play = VotePlayer::getVotePlayer($where);
        return $this->responseData($play);
    }

    //参赛者详情
    public function player(Request $request)
    {
        if (!$request->player_id) {
            return $this->responseFailed('player_id必传');
        }
        $player = VotePlayer::find($request->player_id);
        if ($player) {
            $player->increment('view_player');
            $player = $player->toArray();
            $player['timestamp'] = Vote::getVoteTime($player['vote_id']);
            return $this->responseData($player);
        }
        return $this->responseFailed('无此参赛者');
    }

    //投票接口
    public function userVote(Request $request)
    {
        if (!$request->player_id) {
            return $this->responseFailed('player_id必传');
        }
        $user = auth()->user();
        $where = [
            ['user_id', '=', $user['id']],
            ['player_id', '=', $request->player_id],
            ['vote_id', '=', $request->vote_id],
        ];
        if (VoteLog::isDayVote($where)) {
            return $this->responseFailed('每天最多投3次');
        }
        $vote = Vote::find($request->vote_id);
        if (!$vote) {
            return $this->responseFailed('活动不存在');
        }
        if ($vote['vote_status'] != 1) {
            return $this->responseFailed('活动已停止');
        }
        $player = VotePlayer::find($request->player_id);
        if (!$player) {
            return $this->responseFailed('选手不存在');
        }
        $vote->increment('vote_count');
        $player->increment('vote_player');
        $log = new VoteLog();
        $log->user_id = $user['id'];
        $log->player_id =  $request->player_id;
        $log->vote_id =  $request->vote_id;
        if ($log->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    //参赛者报名接口
    public function addPlayer(Request $request)
    {
        $input = $request->only([
            'vote_id',
            'player_name',
            'player_group',
            'player_area',
            'img',
        ]);
        $validator = Validator::make($input, [
            'vote_id' => 'required',
            'player_name' => 'required',
            'player_group' => 'required',
            'player_area' => 'required',
            'img' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->responseFailed($validator->errors()->first());
        }
        $input['no'] = '3'.rand(1000000,9999999);
        $user = auth()->user();
        $input['user_id'] = $user['id'];
        $where = [
            ['player_name', '=', $input['player_name']],
            ['user_id', '=', $user['id']],
        ];
        $player = VotePlayer::where($where)->first();
        if ($player) {
            return $this->responseFailed('您已报名');
        }
        $player = new VotePlayer();
        foreach($input as $key => $val){
            $player->$key = $val;
        }
        if ($player->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }
}